<?php

namespace App\Controller\Wap;

use App\Entity\ShopGoods;
use App\Entity\UserLikesProduct;
use App\Model\AreaAgentModel;
use App\Model\AreaModel;
use App\Model\CityModel;
use App\Model\MoneyExModel;
use App\Model\MoneyLogModel;
use App\Model\MoneyModel;
use App\Model\ProvinceModel;
use App\Model\ShopGoodsModel;
use App\Model\UserDiyLevelupLimitModel;
use App\Model\UserDiyLevelUpModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseConfig;
use App\Base\BaseController4User;
use BaseExt\QRcode;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserInviteExModel;
use App\Model\LazyCommon\WechatModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/wap")
 */
class AreaController extends BaseController4User
{

    /**
     * @route("/area/scan",name="UserAppAreaScan")
     */
    public function UserAppAreaScan()
    {

        return $this->render("App/Wap/area_scan.php.twig",$this->data);
    }




    /**
     * @route("/area/scan/get",name="UserAppAreaScanGet")
     */
    public function UserAppAreaScanGet()
    {
        if(!$province = $this->getParamRequired("province")){return $this->json_out(0,"请选择省市数据");}
        if(!$city = $this->getParamRequired("city")){$city = 0;}
        if(!$area = $this->getParamRequired("area")){$area = 0;}

        $uid = $this->needToBeUser();
        $AreaAgentModel = new AreaAgentModel($this->getDoctrine());
        $rst = $AreaAgentModel->userScan($uid,$province,$city,$area);
        return $this->json_out(1,"数据获取成功",$rst);
    }


    /**
     * @route("/area/sale",name="UserAppAreaSale")
     */
    public function UserAppAreaSale()
    {

        $uid = $this->needToBeUser();
        //获取本人所在的代理区域
        $AreaAgentModel = new AreaAgentModel($this->getDoctrine());

        $AreaAgent = $AreaAgentModel->getAreaForUser($uid);
//        var_dump($AreaAgent);
        $this->data["my"] = [];
        foreach ($AreaAgent as $item) {
            if ($item->getArea() != 0 ) {
                //是县区代理
                $data["name"] = (new AreaModel($this->getDoctrine()))->getName($item->getArea());
                $rst = $AreaAgentModel->getSaleData4Agent($item->getProvince(),$item->getCity(),$item->getArea());
                if (!$rst) {
                    $rst = 0;
                }else{
                    $rst = round($rst[0]["money"],2);
                }
                $data["data"] = $rst;
                $this->data["my"][] = $data;
                continue;
            }
            if ($item->getCity() != 0 ) {
                //是城市代理
                $data["name"] = (new CityModel($this->getDoctrine()))->getName($item->getCity());
                $rst = $AreaAgentModel->getSaleData4Agent($item->getProvince(),$item->getCity(),$item->getArea());

                if (!$rst) {
                    $rst = 0;
                }else{
                    $rst = round($rst[0]["money"],2);
                }
                $data["data"] = $rst;
                $this->data["my"][] = $data;
                continue;
            }else{
                //是省级代理
                $data["name"] = (new ProvinceModel($this->getDoctrine()))->getName($item->getProvince());
                $rst = $AreaAgentModel->getSaleData4Agent($item->getProvince(),$item->getCity(),$item->getArea());
                if (!$rst) {
                    $rst = 0;
                }else{
                    $rst = round($rst[0]["money"],2);
                }
                $data["data"] = $rst;
                $this->data["my"][] = $data;
//                continue;
            }

        }
        return $this->render("App/Wap/area_sale.php.twig",$this->data);
    }


    /**
     * @route("/area/sale/get",name="UserAppAreaSaleGet")
     */
    public function UserAppAreaSaleGet()
    {
        if(!$province = $this->getParamRequired("province")){return $this->json_out(0,"请选择省市数据");}
        if(!$city = $this->getParamRequired("city")){$city = 0;}
        if(!$area = $this->getParamRequired("area")){$area = 0;}

        $uid = $this->needToBeUser();
        $AreaAgentModel = new AreaAgentModel($this->getDoctrine());
        $rst = $AreaAgentModel->userSale($uid,$province,$city,$area);



        return $this->json_out(1,"数据获取成功",$rst);
    }



}

